package gcu.module.room.dao;

import gcu.module.room.dao.mapper.RoomMapper;
import gcu.module.room.daomain.Room;
import gcu.util.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by haol on 2016/9/26.
 */
@Repository
public class RoomDao {
      @Autowired
      private RoomMapper mapper;

      private Logger logger = LoggerFactory.getLogger(RoomDao.class);

      public void addRoom(Room room) {
            try {
                  mapper.addRoom(room);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }
      public void setRoomIsIn(int id,int isOrNot,Integer ownerId) {
            try {
                  mapper.setRoomIsIn(id,isOrNot,ownerId);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }

      public void deleteRoom(int id) {
            try {
                  mapper.deleteRoom(id);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }
      public void deleteRoom(List list) {
            try {
                  mapper.deleteRoomByList(list);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }
      public void deleteRoomByBuild(String buildName) {
            try {
                  mapper.deleteRoomByBuild(buildName);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }
      public void updateRoom(Room room) {
            try {
                  mapper.updateRoom(room);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
            }
      }


      public List<Room> queryBuildList(Room room, Pager pager) {
            try {
                  int skip = (pager.getCurrentPage() - 1) * pager.getPageSize();
                  int pageSize = pager.getPageSize();
                  pager.setTotalSize(mapper.count(room));
                  return mapper.query(room,skip,pageSize);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
                  return null;
            }
      }

      public Room queryRoomById(Integer id) {
            try {
                  return mapper.queryRoomById(id);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
                  return null;
            }
      }
      public Room queryRoomByNameAndBuildName(String buildName,String roomNum) {
            try {
                  return mapper.queryRoomByNameAndBuildName(buildName,roomNum);
            } catch (Exception e) {
                  logger.error(e.getMessage());
                  e.printStackTrace();
                  return null;
            }
      }
}
